﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmRoles : DockContent
    {
        private DialogResult retVal;

        UserClient _wcfUser;
        RoleRightClient _wcfRole;
        public frmRoles()
        {
            this.KeyPreview = true;
            InitializeComponent();
            retVal = DialogResult.Cancel;
            LoadLaguage();
            _wcfUser = Service.User();
            _wcfRole = Service.RoleRight();
            try
            {
                LoadRoleList();
                dgvModuleRights.RowHeadersVisible = false;
                dgvModuleRights.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        private void LoadLaguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel1);
        }

        private void LoadRoleList()
        {
            try
            {
                string[] strRole = _wcfUser.User_GetAllRoles();
                foreach (string s in strRole)
                {
                    if (s.Substring(0, 3).ToLower().Contains("hrm"))
                        lstRoles.Items.Add(s);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            lstRoles.Enabled = false;
            dgvModuleRights.Enabled = false;
            txtRoleName.Text = "";
            txtRoleName.BackColor = Color.White;
            txtRoleName.ReadOnly = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtRoleName.Text.Trim().Length == 0)
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (!txtRoleName.ReadOnly)
                {
                    if (_wcfUser.User_CreateRole(txtRoleName.Text).ToLower() == "true")
                    {
                        lstRoles.Enabled = true;
                        dgvModuleRights.Enabled = true;
                        txtRoleName.ReadOnly = true;
                        txtRoleName.BackColor = this.BackColor;
                        lstRoles.Items.Add(txtRoleName.Text);
                        lstRoles.SelectedItem = txtRoleName.Text;
                        LoadRoleRightList(txtRoleName.Text);
                        retVal = DialogResult.OK;
                    }
                }
                else
                {
                    string RoleID, ModuleID;
                    bool ViewRight, UpdateRight, CreateRight, DeleteRight, PrintRight;
                    bool result = true;
                    dgvModuleRights.EndEdit();
                    foreach (DataGridViewRow row in dgvModuleRights.Rows)
                    {
                        RoleID = row.Cells["RoleID"].Value.ToString();
                        ModuleID = row.Cells["ModuleID"].Value.ToString();

                        ViewRight = Convert.ToBoolean(row.Cells["ViewRight"].Value);
                        UpdateRight = Convert.ToBoolean(row.Cells["UpdateRight"].Value);
                        CreateRight = Convert.ToBoolean(row.Cells["CreateRight"].Value);
                        DeleteRight = Convert.ToBoolean(row.Cells["DeleteRight"].Value);
                        PrintRight = Convert.ToBoolean(row.Cells["PrintRight"].Value);

                        if (RoleID.Trim().Length > 0)
                        {
                            if (!_wcfRole.Update(RoleID, ModuleID, ViewRight, CreateRight, UpdateRight, PrintRight, DeleteRight))
                                result = false;
                        }
                        else
                        {
                            if (_wcfRole.Insert(txtRoleName.Text, ModuleID, ViewRight, CreateRight, UpdateRight, PrintRight, DeleteRight))
                                row.Cells["RoleID"].Value = txtRoleName.Text;
                            else
                                result = false;
                        }
                    }

                    if (result)
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    else
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        private void lstRoles_Click(object sender, EventArgs e)
        {
            if (lstRoles.SelectedIndex >= 0)
            {
                txtRoleName.Text = lstRoles.SelectedItem.ToString();
                LoadRoleRightList(txtRoleName.Text);
            }
        }

        private void LoadRoleRightList(string RoleID)
        {
            try
            {
                dgvModuleRights.DataSource = null;
                DataTable dt;
                dt = _wcfRole.GetAllByRole(RoleID).Tables[0];
                if (dt.Rows.Count == 0)
                    dt = _wcfRole.GetAllModule().Tables[0];
                dgvModuleRights.Rows.Clear();
                dgvModuleRights.DataSource = dt;

                dgvModuleRights.Columns["RoleID"].Visible = false;
                dgvModuleRights.Columns["ModuleID"].Visible = false;
                dgvModuleRights.Columns["ModuleName"].HeaderText = UpdateUI.GetString("chucnang");
                dgvModuleRights.Columns["ViewRight"].HeaderText = UpdateUI.GetString("xem");
                dgvModuleRights.Columns["UpdateRight"].HeaderText = UpdateUI.GetString("capnhat");
                dgvModuleRights.Columns["CreateRight"].HeaderText = UpdateUI.GetString("taomoi");
                dgvModuleRights.Columns["DeleteRight"].HeaderText = UpdateUI.GetString("xoa");
                dgvModuleRights.Columns["PrintRight"].HeaderText = UpdateUI.GetString("inra");

                dgvModuleRights.Columns["ModuleName"].Width = 200;
                dgvModuleRights.Columns["ViewRight"].Width = 70;
                dgvModuleRights.Columns["UpdateRight"].Width = 70;
                dgvModuleRights.Columns["CreateRight"].Width = 70;
                dgvModuleRights.Columns["DeleteRight"].Width = 70;
                dgvModuleRights.Columns["PrintRight"].Width = 70;

                dgvModuleRights.Columns["ModuleName"].Frozen = true;

                dgvModuleRights.Columns["ModuleName"].ReadOnly = true;
                dgvModuleRights.Columns["ViewRight"].ReadOnly = false;
                dgvModuleRights.Columns["UpdateRight"].ReadOnly = false;
                dgvModuleRights.Columns["CreateRight"].ReadOnly = false;
                dgvModuleRights.Columns["DeleteRight"].ReadOnly = false;
                dgvModuleRights.Columns["PrintRight"].ReadOnly = false;


            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstRoles.SelectedIndex >= 0)
            {
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string result = _wcfUser.User_DeleteRole(lstRoles.SelectedItem.ToString());
                    if (result.ToLower() == "true")
                    {
                        _wcfRole.Delete(lstRoles.SelectedItem.ToString());
                        lstRoles.Items.Remove(lstRoles.SelectedItem.ToString());
                        txtRoleName.Text = "";
                        retVal = DialogResult.OK;
                    }
                    else
                        MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa") + result, UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

        }

        private void frmRoles_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfRole.Close();
            _wcfUser.Close();
            this.DialogResult = retVal;
        }

        private void frmRoles_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.DialogResult = retVal;
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(sender, e);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDelete_Click(null, null);
                    break;
            }

        }

        private void frmRoles_Load(object sender, EventArgs e)
        {
            if (lstRoles.Items.Count > 0)
            {
                lstRoles.SelectedIndex = 0;
                txtRoleName.Text = lstRoles.SelectedItem.ToString();
                LoadRoleRightList(txtRoleName.Text);
            }
        }

        private void lstRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadRoleRightList(txtRoleName.Text);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}